package com.hmyuan.controller.back;

import java.io.FileNotFoundException;
import java.nio.charset.Charset;
import java.sql.Connection;
import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.dbcp.BasicDataSource;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.jdbc.ScriptRunner;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.context.support.WebApplicationContextUtils;

import com.hmyuan.domain.Sys_resources;
import com.hmyuan.domain.Sys_user;
import com.hmyuan.service.SysResourcesService;
import com.hmyuan.service.SysUserService;

/**
 * CommonController
 * @author suxiaofei
 *
 */
@Controller
@RequestMapping("/back")
public class CommonController {
	
	@Resource
	SysResourcesService sysResourcesService;
	@Resource
	SysUserService sysUserService;
	/**
	 *首页 
	 */
	@RequestMapping("/index")
	public String index(HttpServletRequest request,Model model){
		
		Sys_user currentuser = (Sys_user) request.getSession().getAttribute("currentuser");
		List<Sys_resources> list = null;
		//超级管理员用户拥有所用用户权限
		if("sysadmin".equals(currentuser.getAccountname())){
			Sys_resources sys_resources = new Sys_resources();
			//sys_resources.setIshide(0);//查询未被隐藏的数据
			list = sysResourcesService.selectList(sys_resources);
		}else{
			list = sysResourcesService.selectListOfUserAndRoleByUserid(currentuser.getId());
		}
		//将菜单遍历为树形结构
		List<Sys_resources> sysList = new ArrayList<>();
		for (Sys_resources sys : list) {
			for (Sys_resources sys2 : list) {
				if(sys.getParentid()!=null&&sys.getParentid()==sys2.getId()){
					if(sys2.getNodes()==null){sys2.setNodes(new ArrayList<Sys_resources>());}
					sys2.getNodes().add(sys);
				}
			}
			if(null!=sys.getLevel()&&sys.getLevel()==1){//判断是否为主目录
				sysList.add(sys);
			}
		}
		request.setAttribute("sysList", sysList);
		
		return "/back/index";
	}
	/**
	 *登录页面
	 */
	@RequestMapping(value="/login", method = RequestMethod.GET)
	public String login(HttpServletRequest request,Model model){
		return "/back/login";
	}
	/**
	 *登录页面01
	 */
	@RequestMapping(value="/login01", method = RequestMethod.GET)
	public String login01(HttpServletRequest request,Model model){
		return "/back/login01";
	}
	/**
	 *用户登出
	 */
	@ResponseBody
	@RequestMapping(value="/loginOut", method = RequestMethod.POST)
	public String loginOutPost(HttpServletRequest request,Model model){
		request.getSession().invalidate();//销毁session
		return "success";
	}
	/**
	 *用户登出
	 */
	@RequestMapping(value="/loginOut", method = RequestMethod.GET)
	public String loginOutGet(HttpServletRequest request,Model model){
		request.getSession().invalidate();//销毁session
		return "/back/login";
	}
	/**
	 *登录页面提交
	 */
	@ResponseBody
	@RequestMapping(value="/login", method = RequestMethod.POST)
	public String loginSubmit(HttpServletRequest request,Model model,Sys_user sys_user){
		if(null==sys_user.getAccountname()||sys_user.getAccountname().equals("")){
			return "账号不能为空！";
		}else if(null==sys_user.getPassword()||sys_user.getPassword().equals("")){
			return "密码不能为空！";
		}
		Sys_user user = sysUserService.selectListByAccountname(sys_user.getAccountname());
		if(null!=user&&sys_user.getPassword().equals(user.getPassword())){
			request.getSession().setAttribute("currentuser", user);
			return "success";
		}else{
			return "登陆失败！用户名或密码错误！";
		}
	}
	/**
	 *系统初始化
	 * @throws FileNotFoundException 
	 */
	@ResponseBody
	@RequestMapping(value="/systeminit")
	public String systeminit(HttpServletRequest request,Model model){
		ServletContext servletContext=request.getSession().getServletContext();
		ApplicationContext ac2 = WebApplicationContextUtils.getWebApplicationContext(servletContext);
		BasicDataSource bds2 = (BasicDataSource) ac2.getBean("dataSource");
		try {
            Connection conn =bds2.getConnection();
            ScriptRunner runner = new ScriptRunner(conn);
            Resources.setCharset(Charset.forName("utf8")); //设置字符集,不然中文乱码插入错误
            runner.setLogWriter(null);//设置是否输出日志
            runner.runScript(Resources.getResourceAsReader("sql/init.sql"));
            runner.closeConnection();
            if(!conn.isClosed()){
            	conn.close();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
		return "系统初始化成功";
	}
}
